import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { setDocumentText } from '@/utils/utils'

import IndexView from '@/views/index/IndexView.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.VITE_BASE_URL),
  routes: [
    {
      path: '/',
      name: 'index',
      component: IndexView,
      meta: {
        title: '首页',
      },
    },
    {
      path: '/chat',
      name: 'chat',
      component: () => import('@/views/chat/DefaultView.vue'),
      children: [
        {
          path: 'index',
          name: 'chatIndex',
          meta: {
            title: 'AI 对话',
          },
          component: () => import('@/views/chat/IndexView.vue'),
        },
        {
          path: ':id',
          name: 'chatChat',
          component: () => import('@/views/chat/ChatView.vue'),
        },
      ],
    },
    {
      path: '/assistant',
      name: 'assistant',
      component: () => import('@/views/assistant/AssistantView.vue'),
      children: [
        {
          path: 'list',
          name: 'assistantList',
          meta: {
            title: '创作助手',
          },
          component: () => import('@/views/assistant/ListView.vue'),
        },
        {
          path: ':id',
          name: 'assistantChat',
          component: () => import('@/views/assistant/ChatView.vue'),
        },
      ],
    },
    {
      path: '/visrole',
      name: 'visrole',
      component: () => import('@/views/visrole/VisroleView.vue'),
      children: [
        {
          path: 'list',
          name: 'visroleIndex',
          meta: {
            title: '角色列表',
          },
          component: () => import('@/views/visrole/IndexView.vue'),
        },
        {
          path: ':id',
          name: 'visroleChat',
          component: () => import('@/views/visrole/ChatView.vue'),
        },
      ],
    },
    {
      path: '/auth',
      name: 'auth',
      component: () => import('@/views/auth/AuthView.vue'),
      children: [
        {
          path: 'login',
          name: 'authLogin',
          meta: {
            title: '登录',
          },
          component: () => import('@/views/auth/LoginView.vue'),
        },
        {
          path: 'register',
          name: 'authRegister',
          meta: {
            title: '注册',
          },
          component: () => import('@/views/auth/RegisterView.vue'),
        },
        {
          path: 'forget',
          name: 'authForget',
          meta: {
            title: '忘记密码',
          },
          component: () => import('@/views/auth/ForgetView.vue'),
        },
      ],
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('@/views/user/UserView.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: 'index',
          name: 'userIndex',
          meta: {
            title: '个人中心',
          },
          component: () => import('@/views/user/IndexView.vue'),
        },
        {
          path: 'info',
          name: 'userInfo',
          meta: {
            title: '账户信息',
          },
          component: () => import('@/views/user/InfoView.vue'),
        },
        {
          path: 'charge',
          name: 'userCharge',
          meta: {
            title: '充值套餐',
          },
          component: () => import('@/views/user/ChargeView.vue'),
        },
        {
          path: 'score_log',
          name: 'userScoreLog',
          meta: {
            title: '积分日志',
          },
          component: () => import('@/views/user/ScoreLogView.vue'),
        },
        {
          path: 'award',
          name: 'userAward',
          meta: {
            title: '签到奖励',
          },
          component: () => import('@/views/user/AwardView.vue'),
        },
        {
          path: 'invite',
          name: 'userInvite',
          meta: {
            title: '邀请好友',
          },
          component: () => import('@/views/user/InviteView.vue'),
        },
      ],
    },
    {
      path: '/document/help',
      name: 'documentHelp',
      component: () => import('@/views/document/help/FrameView.vue'),
      children: [
        {
          path: ':id',
          name: 'documentHelpRead',
          component: () => import('@/views/document/help/ReadView.vue'),
        },
      ],
    },
  ],
})

//前置守卫
router.beforeEach(async (to, from, next) => {
  console.log(from.fullPath, '==>', to.fullPath)
  const authStore = useAuthStore()

  if (!authStore.isAuthenticated && localStorage.getItem('token')) {
    await authStore.check()
  }

  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    next({
      name: 'authLogin',
      query: { redirect: encodeURIComponent(to.fullPath) },
    })
  } else if (to.meta?.title) {
    setDocumentText(to.meta.title)
  }
  next()
})

//后置守卫
router.afterEach((to, from) => {
  //恢复背景色
  document.body.style.backgroundColor = ''
})

export default router
